/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/



#include "stdafx.h"
//#include "基本类型.h"
#include "字符串数值互换.h"



int32 f_字符串转I32(const std::wstring& text) {
	uint32 num = uint32(text.size());
	if(!num) return 0;
	num = DEF_Min(num, 10);

	int32 value = 0;

	int32 正负 = 0;
	if (text.at(0) == L'-') {
		正负 = 1;
	}

	uint32 步进 = 1;
	for (int16 i = num - 1; i >= 正负; --i) {
		value += int32(text[i] - 48) * 步进;
		步进 *= 10;
	}
	
	return 正负 ? -value : value;
}

int32 f_字符串转I32(const std::string& text) {
	uint32 num = uint32(text.size());
	if (!num) return 0;
	num = DEF_Min(num, 10);

	int32 value = 0;

	int32 正负 = 0;
	if (text.at(0) == '-') {
		正负 = 1;
	}

	uint32 步进 = 1;
	for (int16 i = num - 1; i >= 正负; --i) {
		value += int32(text[i] - 48) * 步进;
		步进 *= 10;
	}

	return 正负 ? -value : value;
}


float32 f_字符串转F32(const std::wstring& text) {
	int32 num = int32(text.size());
	if (!num) return 0;
	num = DEF_Min(num, 11);
	float32 value = 0;


	int16 小数点位置 = num;
	for (uint16 i = 0; i < num; ++i) {
		if (text[i] == '.') {
			小数点位置 = i;
			break;
		}
	}
	//uint16 位数 = num - 小数点位置;

	

	int32 正负 = 0;
	if (text.at(0) == L'-') {
		正负 = 1;
		//i = 小数点位置;
	}

	uint32 步进 = 1;
	//int16 i = 位数;
	int16 位数 = DEF_Max(小数点位置 - 1, 0);
	for (int16 i = 位数; i >= 正负; --i) {
		value += (int32(text[i]) - 48) * 步进;
		步进 *= 10;
	}
	
	//float32 小数 = 1.0 / 10.0;
	int32 小数 = 10;
	for (int16 i = 小数点位置 + 1; i < num; ++i) {
		//value += (int32(text[正负 + i]) - 48) * 小数;
		value += float32(int32(text[i]) - 48) / 小数;
		小数 *= 10;
		//小数 *= 0.1f;
	}



	return 正负 ? -value : value;
}







